package bjut.core.postprocessor;

import bjut.core.aware.ApplicationContextAware;
import bjut.core.context.ApplicationContext;
import bjut.core.exception.BeansException;

/**
 * @Author: zhuzhiming
 * @Date: 2022/6/30 21:25
 */
public class ApplicationContextAwareProcessor implements BeanPostProcessor {

    private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postBeanPostProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof ApplicationContextAware){
            ((ApplicationContextAware) bean).setApplicationContext(applicationContext);
        }
        return bean;
    }

    @Override
    public Object postBeanPostProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
